/**
 * zmn员工模块-员工大数据报表数据权限树状列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/06/11 18:12
 */
layui.define(['form', 'table', 'zmnApp', 'treeTable2'], function (exports) {
    let $ = layui.jquery;
    let form = layui.form;
    let layer = layui.layer;
    let treeTable = layui.treeTable2;
    let zmnApp = layui.zmnApp;

    treeTable.render({
        elem: '#zmn-table',
        toolbar: '#zmn-table-tool',
        defaultToolbar: ['filter', 'print'],
        //url: '/role/permit/ajaxTree.action?roleId=' + json.roleId,
        cellMinWidth: 50,
        height: 'full-272',
        // size: 'sm',
        tree: {
            iconIndex: 1,           // 折叠图标显示在第几列
            isPidData: true,        // 是否是id、pid形式数据
            idName: 'dataId',       // id字段名称
            pidName: 'parentId'     // pid字段名称
        },
        cols: [[
            {field: '', title: '', width: 50, align: 'center', sort: false, templet: function(d) {
                    return d.level > 0 ? '<input type="checkbox" name="dpermit" value="' + d.dataId + '" ' + (d.checked ? 'checked' : '') + ' lay-skin="primary"/>' : '';
                }},
            // {field: 'dataId', title: '一级/二级报表菜单ID', width: 80, align: 'center', sort: false},
            {field: 'dataName', title: '一级/二级/三级报表菜单', width: 240, sort: false},
            {field: '', title: '四级报表菜单列表', minWidth: 180, align: 'left', singleLine: false, templet: function(d) {
                    let html = '';
                    d.subors.forEach(function (item, i) {
                        html += '<input type="checkbox" name="dpermit" data-type="' + d.parentId + '" value="' + item.dataId + '" ' + (item.checked ? 'checked' : '') + ' title="' + item.dataName + '" lay-skin="primary"/>'
                    });
                    return html;
                }}
        ]],
        reqData: function (data, callback) {     // 数据加载后回调
            $.get(zmnApp.SYS_BASE_URL +'/staff/dpermit/cube/ajaxTree.action?staffId=' + json.staffId, function (res) {
                if(res.status == 200) {
                    res.data.forEach(function(item) {
                        if(item.level <= 1) {
                            item.open = true;
                        }
                    });
                    callback(res.data)
                } else {
                    callback(res.message)
                };
            });
        }
        // end: function(e){},
    });

    // 授权选中/清除全部
    treeTable.on("toolbar(zmn-table)", function (obj) {
        if(obj.event == "assign") {
            layui.layer.confirm('确认提交授权吗？', function() {
                let $ = layui.$;
                var $dpermitIds = $('input[name="dpermit"]');
                var dpermitIds = new Array();
                if ($dpermitIds.length > 0) {
                    $dpermitIds.each(function (i) {
                        let $this = $(this);
                        if ($this.prop('checked')) {
                            dpermitIds.push($(this).val());
                        }
                    });
                }
                if (dpermitIds == null || dpermitIds.length == 0) {
                    layui.layer.msg("请选择要分配的数据权限");
                    return;
                }
                zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL +"/staff/dpermit/modify/cube/assign.action", {
                    staffId: json.staffId,
                    dpermitIds: dpermitIds.join(",")
                }, 0);
            });
        } else if(obj.event == "clear") {
            layui.layer.confirm('确认清除全部大数据报表数据权限吗？', function () {
                let $ = layui.$;
                zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL +"/staff/dpermit/modify/cube/clear.action", {
                    staffId: json.staffId
                }, 1500);
            });
        }
    });

    //对外暴露的接口
    exports('staff_dpermit_cube_tree', {});
});